package com.maxatime.web.controller;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//线程
public class Thread02 extends Thread{
	 
	/*
	 * 利⽤继承中的特点
	 * 将线程名称传递 进⾏设置
	 */
	 public Thread02(String name) {
	 super(name);
	 }
	
	private int num = 8;//数量
	
	Lock lock = new ReentrantLock();//lock锁
	
	//重写run方法，run 表示这个线程需要执行的任务
    @Override
    public void run() {
        System.out.println("Thread02+++!"+Thread.currentThread().getName());//Thread.currentThread().getName()=获取线程的名字
        while (true) {
        	lock.lock();//加lock锁
        	if(num > 0) {
        		try {
        			 Thread.sleep(1000);//间隔时间
        		} catch (InterruptedException e) {
        			 e.printStackTrace();
        		}
        		// 获取当前线程对象的名字
        		 String name = Thread.currentThread().getName();
        		System.out.println(getName() + "正在卖:" + num--);
        	}
        		lock.unlock();//解lock锁
        	}
        }
    
//    public static void main(String[] args) {
//    	Thread02 a1 =new Thread02();
//    	Thread02 a2 =new Thread02();
//    	Thread02 a3 =new Thread02();
//    	Thread02 a4 =new Thread02();
//    	a1.start();
//    	a2.start();
//    	a3.start();
//    	a4.start();
//    	
//    }
}
